﻿using System;
using System.Data.Entity;

namespace Project.Data.Repository
{
    public class DbContextFactory<TContext> : IDbContextFactory<TContext>, IDisposable
        where TContext : DbContext
    {

        private readonly Type _t;
        private TContext _context;

        public DbContextFactory()
        {
            _t = typeof(TContext);
        }

        public TContext GetDbContext()
        {
            return _context ?? (_context = (TContext)Activator.CreateInstance(_t));
        }

        public void Dispose()
        {
            _context?.Dispose();

            GC.SuppressFinalize(this);
        }
    }
}